package com.intmall.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intmall.train.common.context.LoginMemberContext;
import com.intmall.train.common.domain.resp.CommonPageResp;
import com.intmall.train.common.util.SnowUtil;
import com.intmall.train.member.domain.po.Passenger;
import com.intmall.train.member.domain.po.PassengerExample;
import com.intmall.train.member.domain.req.PassengerQueryReq;
import com.intmall.train.member.domain.req.PassengerSaveReq;
import com.intmall.train.member.domain.resp.PassengerQueryResp;
import com.intmall.train.member.mapper.PassengerMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PassengerService {
    @Resource
    private PassengerMapper passengerMapper;

    public void save(PassengerSaveReq req) {
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(req.getId())) {
            Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        } else {
            PassengerExample passengerExample = new PassengerExample();
            PassengerExample.Criteria criteria = passengerExample.createCriteria();
            criteria.andMemberIdEqualTo(LoginMemberContext.getId());
            criteria.andIdEqualTo(req.getId());
            List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
            if (IterUtil.isNotEmpty(passengers)) {
                Passenger passenger = passengers.get(0);
                passenger.setName(req.getName());
                passenger.setIdCard(req.getIdCard());
                passenger.setType(req.getType());
                passenger.setUpdateTime(now);
                passengerMapper.updateByPrimaryKeySelective(passenger);
            }
        }
    }

    public void delete(Long id) {
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        criteria.andIdEqualTo(id);
        passengerMapper.deleteByExample(passengerExample);
    }

    public CommonPageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        if (ObjectUtil.isNotNull(req.getMemberId())) {
            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        PageHelper.startPage(req.getPage(), req.getLimit());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);

        List<PassengerQueryResp> list = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);
        return CommonPageResp.SUCCESS("", list, pageInfo);
    }
}
